package com.niuquan.activitie;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.niuquan.adapter.SpinnerAdapter;
import com.niuquan.base_data.Messages;
import com.niuquan.base_function.cache_core;
import com.niuquan.function.UserinfoCache;
import com.niuquan.function.findniushi_Nettool;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by lewen on 2015/5/24.
 */
public class publishActivity extends Activity {
    EditText title = null;
    Spinner spinner = null;
    EditText description = null;
    TextView time = null;
    EditText niubi = null;
    Button publish = null;
    Button cancel = null;
    UserinfoCache userinfo = null;
    Calendar calendar = null;
    Context context = null;
    DatePickerDialog dialog = null;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.publish_layout);
        context = this.getApplicationContext();
        userinfo = UserinfoCache.getInstance(this.getApplicationContext());
        userinfo.setUserInfo("userId","1");
        title = (EditText)findViewById(R.id.fanihuotitleedit);
        spinner = (Spinner)findViewById(R.id.faniuhuotypespinner);
        ArrayList<String> citylist= new ArrayList<String>();
        citylist.add("c++");
        citylist.add("java");
        SpinnerAdapter cityadapter = new SpinnerAdapter(this,citylist);
        spinner.setAdapter(cityadapter);
        description = (EditText)findViewById(R.id.faniuhuomiaosuedit);;
        time = (TextView)findViewById(R.id.faniuhuodeadline);
        niubi = (EditText)findViewById(R.id.faniuhuomoneyedit);
        publish = (Button)findViewById(R.id.confirmfahuo);
        cancel = (Button)findViewById(R.id.cancelfahuo);
        calendar = Calendar.getInstance();
        DatePickerDialog.OnDateSetListener dateListener =
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker,
                                          int year, int month, int dayOfMonth) {

                        //Calendar 月份是从0开始,所以month要加1
                        time.setText(year + "-" + (month+1) + "-" + dayOfMonth);
                    }
                };
        dialog = new DatePickerDialog(this,
                dateListener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.show();
            }
        });
        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    new findniushi_Nettool().getniushi(getmessage());
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
                finish();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public Messages getmessage()
    {
        ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
        list.add( new BasicNameValuePair("userId",userinfo.getUserInfo("userId")));
        list.add( new BasicNameValuePair("tag",spinner.getSelectedItem(). toString()));
        list.add( new BasicNameValuePair("name",title.getText().toString()));
        list.add( new BasicNameValuePair("description",description.getText().toString()));
        list.add( new BasicNameValuePair("endtime",time.getText().toString()));
        list.add( new BasicNameValuePair("getcoin",niubi.getText().toString()));
        return new Messages(list);
    }
}
